class GwtController<ApplicationController
  @@LOG_FLAG='BuyGwtController'
  def refresh_sip_sessionid
          #为让用户二次登录进行了sip_session更新处理
          new_sip_sessionid =  WWW::Tkd::Gold::Model::TbUser.refresh_sip_sessionid session[:id]
          session[:sip_sessionid]=new_sip_sessionid
  end
 #授权成功，进行第2次检查
  def success
    #根据登录的淘宝账号进行更新
    taobao_user_name = params[:isp_username]
    if taobao_user_name!=nil
       ori_domain_login_id = session[:domain_login_id]
       if taobao_user_name!=ori_domain_login_id
          refresh_sip_sessionid
          render :text => "尊敬的用户，淘快递仅支持淘宝旺旺用户，当前旺旺帐号:#{ori_domain_login_id}与淘宝网帐号:#{taobao_user_name}不一致，请关闭重新授权。<a href='/gwt_buyer/view'>返回淘快递</a>。谢谢!"
          return
       end
    end
    redirect_to '/gwt_buyer/view'
  end
  #让用户访问淘宝网进行授权
  def login_taobao
     #获得Taobao的buyer Order数据
    params[:fields]='seller_nick,buyer_nick,iid,title,price,pic_path,num,created,type,tid,status,seller_rate,buyer_rate'
    buy_trade = WWW::Ali::Taobao::Trade.new params,session
    buy_items = buy_trade.get_buy_items
    #如果Taobao没有登录成功，预先登录
    login_valid = WWW::Ali::Taobao::Tool.is_login(buy_items)
    if login_valid.success==false
      redirect_to login_valid.url
      return
    end
    redirect_to '/gwt_buyer/view'
  end
  def checkname name
      key = 'cntaobao'
      if name.length>key.length && name[0,key.length]==key
         return true
      end
      return false
  end
  #判定授权是否到期
  def is_valid_login
     params[:fields]='seller_nick,buyer_nick,iid,title,price,pic_path,num,created,type,tid,status,seller_rate,buyer_rate'
    buy_trade = WWW::Ali::Taobao::Trade.new params,session
    buy_items = buy_trade.get_buy_items
    #如果Taobao没有登录成功，预先登录
    login_valid = WWW::Ali::Taobao::Tool.is_login(buy_items)
    if login_valid.success
        get_data buy_items
    end
    return login_valid.success
  end
  #获得淘宝卖家的数据
  def get_data buy_items
        WWW::Tkd::Gold::Model::TbBuyerOrder.save_from_tabao(buy_items,session)#将Tabao订单数据导入到数据库中
  end
end
